﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace BaseFunction
{
    public class LoginHelp : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            this.Error += new System.EventHandler(PageBase_Error);
            base.OnInit(e);
        }


        /// <summary>
        /// 在页面初始化之后触发
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInitComplete(EventArgs e)
        {
            this.Page.Title = BaseFunction.StaticTableClass.SystemConfigHelp.GetSystemConfigValue("systemname") + " - " + BaseFunction.StaticTableClass.SystemConfigHelp.GetSystemConfigValue("companyname");
            base.OnLoadComplete(e);

            IsLogin();
            base.OnInitComplete(e);
        }



        /// <summary>
        /// 系统发生错误
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PageBase_Error(object sender, System.EventArgs e)
        {
            string errMsg;
            Exception currentError = Server.GetLastError();
            errMsg = "<link rel=\"stylesheet\" href=\"/style.css\">";
            errMsg += "<h1>系统错误：</h1><hr/>系统发生错误， " +
                "该信息已被系统记录，请稍后重试或与管理员联系。<br/>" +
                "错误地址： " + Request.Url.ToString() + "<br/>" +
                "错误信息： <font class=\"ErrorMessage\">" + currentError.Message.ToString() + "</font><hr/>" +
                "<b>Stack Trace:</b><br/>" + currentError.ToString();
            Response.Write(errMsg);
            Server.ClearError();

            /***写入系统日志***/
        }


        protected void IsLogin()
        {
            if (System.Web.HttpContext.Current.Session["AgentId"] == null || System.Web.HttpContext.Current.Session["AgentId"] + "" == "")
            {
                StringBuilder Lg = new StringBuilder();
                Lg.Append("<script>");
                Lg.Append("alert('您尚未登录,或者登录已经超时,请重新登录');top.location.href='/Login.aspx'");
                Lg.Append("");
                Lg.Append("</script>");
                System.Web.HttpContext.Current.Response.Write(Lg.ToString());
                System.Web.HttpContext.Current.Response.End();
            }
        }


        /// <summary>
        /// 获取当前登录的用户
        /// </summary>
        /// <returns></returns>
        public int GetSessionAgentId()
        {
            IsLogin();
            int result;
            if (int.TryParse(System.Web.HttpContext.Current.Session["AgentId"] + "", out result))
            {
                result = int.Parse(System.Web.HttpContext.Current.Session["AgentId"] + ""); ;
            }
            return result;
        }

        /// <summary>
        /// 获取当前登录的用户Model
        /// </summary>
        /// <returns></returns>
        public YY.Model.agent_users GetSessionModel()
        {
            IsLogin();
            YY.Model.agent_users model_agent_users = BaseFunction.StaticTableClass.agent_users_Help.bll_agent_users.GetModel(GetSessionAgentId());
            if (model_agent_users == null)
            {
                System.Web.HttpContext.Current.Session["AgentId"] = null;
                return null;
            }
            return model_agent_users;
        }
    }
}
